import { Modal } from 'antd';
import {WeaLocaleProvider, WeaFormItem, WeaSearchGroup} from 'ecCom';
import {WeaSwitch} from 'comsMobx';
const getLabel = WeaLocaleProvider.getLabel;

export const transfStr = (name = '', ids = '', list = [], type = '') => {
	let str = '';
	const mirror = {
		37: "doc",
		prjtsk: "task",
		18: "crm",
		152: "workflow",
		135: "project",
		workplan: "workplan",
		blogTemplate: "blogTemplate"
	}
	list.map(item => {

		if(name === 'Upload' && type === 'image'){
			str += '<img class="formImgPlay" src="' + item.imgSrc + '" data-imgsrc="' + (item.loadlink || item.acclink) + '" />&#8203;'
		}
		if(name === 'Upload' && type === 'file'){
            str += `<a href='javascript:void(0)' onclick='try{opendoc1(${item.fileid},this);return false;}catch(e){}' ondblclick='return false;' unselectable='off' contenteditable='false' style='cursor:pointer;text-decoration:underline !important;margin-right:8px'>${item.filename}</a>
            &nbsp;<a href='javascript:void(0)' onclick='try{downloads(${item.imagefileid},this,"${item.filename}");return false;}catch(e){}' unselectable='off' target='_blank' contenteditable='false' style='cursor:pointer;text-decoration:underline !important;margin-right:8px'>下载(${item.filesize})</a></br>`;
		}
		if(name === 'Browser'){
			str += (type === 'blogTemplate' ? item.name : `<a href='javascript:void(0)'  linkid='${item.id}' linkType='${mirror[type]}' onclick='try{return openAppLink(this,${item.id});}catch(e){}' ondblclick='return false;'  unselectable='off' contenteditable='false' style='cursor:pointer;text-decoration:underline !important;margin-right:8px'>${item.name || item.showname}</a>&nbsp;`)
		}
	})

	return str
}

export const templetChange = (valueNow = '' ,callback,  name = '', ids = '', list = [], type = '') => {
	if(valueNow){
		Modal.confirm({
			title: getLabel(505615,'替换当前内容？'),
			onOk: () => {
				callback(transfStr(name, ids, list, type))
			},
			onCancel: () => {
				
			}
		});
	}else{
		callback(transfStr(name, ids, list, type))
	}
}

let formParams={};
//带表单验证的form数据
export const getFormSearchs = (form, datas, needTigger = false, lableCol, fieldcol,ecId) => {
  const {isFormInit} = form;
  let group = [];


    const formParamsTemp = form.getFormParams();
    for (let i in formParamsTemp){
       formParams[i] = formParamsTemp[i];
    }

  isFormInit && datas && datas.map((c,i)=>{
    let items = [];
    c.items.map(fields => {
      items.push({
        com:(
          <WeaFormItem ecId={`${ecId}_WeaFormItem@8p3yyr@${fields.domkey[0]}`}
            label={`${fields.label}`}
            labelCol={{span: lableCol ? lableCol : `${fields.labelcol}`}}
            wrapperCol={{span: fieldcol ? fieldcol : `${fields.fieldcol}`}}
            error={form.getError(fields)}
            tipPosition="bottom"
          >
            <WeaSwitch ecId={`${ecId}_WeaSwitch@i55wo6@${fields.domkey[0]}`} fieldConfig={fields} form={form} formParams={formParams}/>
          </WeaFormItem>),
        colSpan:1
      })
    });
    if(needTigger){
      group.push(<WeaSearchGroup ecId={`${ecId}_WeaSearchGroup@t2nw15@${i}`} col={1} center needTigger={needTigger} title={c.title} showGroup={c.defaultshow} items={items}/>)
    }else {
      group.push(<WeaSearchGroup ecId={`${ecId}_WeaSearchGroup@hzum0p@$yzblog${i}`} col={1} center needTigger={needTigger} showGroup={c.defaultshow} items={items}/>)
    }
  });
  return group;
}

//删除等信息确认
export const doConfirm = (content, func) => {//确认信息， 方法， 方法参数
  Modal.confirm({
    title: `${getLabel(131329,'信息确认')}`,
    content: content,
    okText: `${getLabel(382958,'确定')}`,
    cancelText:`${getLabel(31129,'取消')}`,
    onOk: () => func && func(),
  })
}

//提示
export const setModal = (content) => {
  Modal.warning({
    title: `${getLabel(15172,'系统提示')}`,
    content: content,
  });
}

//表单验证
export const  onCheck = (form, func) => {
  form.validateForm().then(f=>{
    if(f.isValid){
      func && typeof func == 'function' && func();
    }else{
      f.showErrors();
    }
  });
}
